package com.woniu.sharepark.leasesystem.common.controller;

import com.alipay.api.AlipayApiException;
import com.woniu.sharepark.common.JsonResult;
import com.woniu.sharepark.leasesystem.common.service.OrderFormService;
import com.woniu.sharepark.leasesystem.lessor.pojo.CarPark;
import com.woniu.sharepark.leasesystem.lessor.service.CarParkService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author DELL
 * @date 2019/6/3 17:00
 * version v1.0
 */

@Controller
@RequestMapping("alipay")
public class PayController {

    @Resource
    private CarParkService carParkService;

    @Resource
    private OrderFormService orderFormService;


    /**
     * @param carParkId
     * @return com.woniu.sharepark.leasesystem.lessor.pojo.CarPark
     * @title 按车位id 查找
     * @author DELL
     * @date 2019/6/3 14:53
     **/

    @RequestMapping("findCarPark")
    @ResponseBody
    public CarPark findCarPark(Integer carParkId) {
        CarPark carPark = carParkService.findCarPark(carParkId);
        return carPark;

    }

    /**
    * 创建订单
    * @title createOrderNumber
    * @param
    * @return com.woniu.sharepark.common.JsonResult
    * @author DELL
    * @date 2019/6/12 11:55
    **/

    @RequestMapping("/createOrderNumber")
    @ResponseBody
    public JsonResult createOrderNumber(CarPark carPark){

        JsonResult result = orderFormService.createOrderNumber(carPark);
        return result;
    }
    /**
    *
    * @title 支付
    * @param date
    * @return JsonResult
    * @author DELL
    * @date 2019/6/3 18:41
    **/

    @RequestMapping("payOrder")
    @ResponseBody
    public JsonResult payOrder(@RequestParam(value = "rentDate") Date date,@RequestParam(value = "voucherId",required = false) Integer voucherId) throws AlipayApiException {
        JsonResult result = orderFormService.payOrder(date,voucherId);
        return result;
    }

    /**
     * @param
     * @return java.lang.String
     * @title 充值
     * @author DELL
     * @date 2019/6/3 15:47
     **/

    @RequestMapping(value = "goAlipay", produces = "text/html; charset=UTF-8")
    @ResponseBody
    public String goAlipay(String money) throws AlipayApiException {
       Double inverst = Double.parseDouble(money);
       String url = orderFormService.invest(inverst);
       return url;
    }

    /**
     * @Description: 支付宝异步 通知页面
     */
    @RequestMapping(value = "alipayNotifyNotice")
    @ResponseBody
    public String alipayNotifyNotice(HttpServletRequest request, HttpServletRequest response) throws Exception {

        String result = orderFormService.alipaySuccess(request,response);

        return "success";
    }
}